package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pojo.UserDynamic;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * @author LeXin Huang
 * @date 2021年03月23日 13:11
 */
@Mapper
public interface UserDynamicMapper extends BaseMapper<UserDynamic> {

    /**
     * 查询 id 大于一定值的 的记录
     * @param currentId 指定的下限动态 id
     * @return 动态数组
     */
    @Results(id = "dynamicMap", value = {
            @Result(column = "id", property = "id", javaType = Integer.class, id = true),
            @Result(column = "dynamic_topic", property = "topic", javaType = String.class),
            @Result(column = "dynamic_content", property = "content", javaType = String.class),
            @Result(column = "dynamic_publish_time", property = "publishTime", javaType = Date.class),
            @Result(column = "id", property = "picturesUrl", one = @One(
                    select = "com.example.mapper.UserDynamicPictureMapper.selectByDynamicId")),
            @Result(column = "user_id", property = "publisher", one = @One(columnPrefix = "user_id",
                    select = "com.example.mapper.PublisherMapper.selectById")),
            @Result(column = "user_id", property = "userId")
    })
    @Select("Select * from user_dynamic main_table INNER JOIN " +
            "(Select id from user_dynamic where id > #{current_id} order by id DESC) temp_table " +
            "on temp_table.id = main_table.id " +
            "order by main_table.id DESC, main_table.dynamic_publish_time DESC")
    List<UserDynamic> selectLatestDynamics(@Param("current_id") Integer currentId);

    /**
     * 查询 id 小于 offset 的 size 条记录
     * @param offset id 上限
     * @param size 单次查询的记录数
     * @return 动态内容数组
     */
    @ResultMap(value = "dynamicMap")
    @Select("Select * from user_dynamic main_table INNER JOIN " +
            "(Select id from user_dynamic where id < #{offset_id} order by id DESC LIMIT #{size}) temp_table " +
            "on temp_table.id = main_table.id " +
            "order by main_table.id DESC, main_table.dynamic_publish_time DESC")
    List<UserDynamic> selectHistoryDynamics(@Param("offset_id") Integer offset, @Param("size") Integer size);

    @ResultMap(value = "dynamicMap")
    @Select("Select * from user_dynamic main_table INNER JOIN " +
            "(Select id from user_dynamic where user_id = #{user_id} and id < #{offset_id} " +
            "order by id DESC LIMIT #{size}) temp_table " +
            "on temp_table.id = main_table.id " +
            "order by main_table.id DESC, main_table.dynamic_publish_time DESC")
    List<UserDynamic> selectUserHistoryDynamics(@Param("user_id") Integer userId,
                                                @Param("offset_id") Integer offset,
                                                @Param("size") Integer size);

    @ResultMap(value = "dynamicMap")
    @Select("Select * from user_dynamic main_table INNER JOIN " +
            "(Select id from user_dynamic where user_id = #{user_id} and id > #{current_id} " +
            "order by id DESC) temp_table " +
            "on temp_table.id = main_table.id " +
            "order by main_table.id DESC, main_table.dynamic_publish_time DESC")
    List<UserDynamic> selectLatestUserDynamics(@Param("user_id") Integer userId, @Param("current_id") Integer current);
}
